<template>
  <div class="kind">
    <div class="back">
      <van-icon name="arrow-left"
                size="0.8rem"
                class="back-icon"
                @click="goback()" />    
      <ul class="type-list">
          <li class="type" v-for="(item,index) in kind" :key="index" @click="selectKind(index,item.name)" :class="{typeBack : index === inx}">
            <!-- <img :src="item.img" alt="" class="icon index-icon"> -->
            <svg class="icon kind-icon" aria-hidden="true" :class="{svgBack : index === inx}">
              <use :xlink:href=item.svg></use>
            </svg>
            <span class="typeNameNone" :class="{typeName : index === inx}">{{item.name}}</span>
          </li>
      </ul>
    </div>
    <div class="pet-list">    
      <div class="pet" v-for="(petItem,index) in diffKindPet" :key="index" @click="gotoPet(petItem.id)">
        <div class="pet-img">
          <van-image :src="petItem.img" alt="">
            <template v-slot:loading>
              <van-loading type="spinner" size="18" />
            </template>
          </van-image>   
        </div>
        <div class="pet-info">
          <h3 class="name">
            <p>{{petItem.pname}}</p>
            <svg class="icon gender" aria-hidden="true" v-if="petItem.gender === 'male'">
              <use xlink:href="#icon-nv"></use>
            </svg>
            <svg class="icon gender" aria-hidden="true" v-if="petItem.gender === 'female'">
              <use xlink:href="#icon-nan"></use>
            </svg>
          </h3>
          <p class="belong">{{petItem.type}}</p>
          <p class="particular">
            <span v-for="(item,index) in petItem.particular" :key="index">{{item.info}}•</span>
          </p>
          <p class="belong">
            来自{{petItem.belong}}
          </p>
        </div>
      </div>
    <div>

      </div>
    </div>
  </div>
</template>
<script>
import { getpetInfo,getAgencyInfo ,getType} from '@/api.js'
export default {
  data() {
    return {
      inx:'',
      pet:'',
      diffKindPet:[],
      petlist:[],
      kind:[]
    }
  },
  created() {
    this.getPet()
    this.getKind()
  },
  methods:{
        async getKind() { 
            try{ 
                const data =  await getType({})
                if(!data.success) return this.$message.error('系统繁忙')
                this.kind = data.data
            }catch(e) { 
                console.log(e);
            }
        },
    gotoPet(id){
     this.$router.push('/pet/'+id)
   },
    selectKind(index,name){
      this.inx = index
      this.pet = name
      this.diffKindPet =this.petlist.filter(item => {
        if(item.kind.includes(this.pet)){
          return item
        }
      })
    },
    async getAgencyInfo(params) {
       await getAgencyInfo(params).then(res => {
         return res.data.real_name
       })
      
    },
    async getPet() {
     const {data} = await getpetInfo()
     this.petlist = data
    //  this.petlist = petlist.map(item => {
    //    return item.belong_name = this.getAgencyInfo({aid:item.belong})
    //  })
     this.inx = JSON.parse(this.$route.params.index)
     this.pet = this.$route.params.name
     this.selectKind(this.inx,this.pet)
    },
    goback () {
      this.$router.go(-1)
    },
  }
}
</script>
<style lang="less" scoped>
.kind {
  position: relative;
  // padding: 0rem 0.5rem 0.5rem;
  width: 100%;
  overflow:hidden;
  .back {
    z-index: 2;
    height: 4.4rem;
    background:white;
    position: fixed;
    width: 100%;
    padding: 0.5rem 0.5rem 0.25rem;
    background-image: linear-gradient(0deg, rgba(255,0,0,0),#ffd6eab0,#fedd97);
    .back-icon{
      margin-bottom: 0.3rem;
      margin-left: -0.3rem;
    }
    .type-list{
      display:flex;
      justify-content: space-between;
      .type{
        border-radius: 1rem;
        width: 1.6rem;
        height: 2.5rem;        
        text-align: center;
        padding: 0.15rem;
        padding-top: 0.18rem;
        svg{
          width: 1.3rem;
          height: 1.3rem;
          border-radius: 1rem;          
          margin-bottom: 0.2rem;
        }
        .svgBack{
          background: white;
        }
      }
      .typeBack{
        background: #74b6ff;
        transition: 0.5s;
      }
      .typeNameNone{
        display: none;
      }
      .typeName{
        transition: 0.5s;
        font-size: 0.35rem;
        color: white;
        display: inline-block;
      }
     
    }
  }
  .pet-list{
    padding: 0rem 0.5rem 0.5rem;
    z-index: 1; 
    margin-top: 4.5rem;
      .pet{
        position: relative;
        height: 7rem;    
        .pet-img{
          border-radius: 0.25rem;
          width: 100%;
          height: 5rem;
          overflow:hidden;
          box-shadow: 2px 3px 6px 0px #656b794d;
          img{
            width: 100%;
          }
        }  
        .pet-info{
          position: absolute;
          bottom: 0.5rem;
          right: 0.5rem;
          line-height: 0.5rem;
          padding: 0.2rem;
          font-size: 0.4rem;
          background: white;                     
          color: #a09ea0;
          width: 68%;
          border-radius: 0.15rem;
          box-shadow: 2px 3px 6px 0px #656b794d;
          .name{
            color: black;
            display: flex;
            justify-content: space-between;
            .gender {
                width: 0.5rem;
                height: 1.5em;
            }
          }
          .particular{
            display: flex;
            justify-content: space-between;
            line-height: 0.75rem;
          }
          .belong{
            font-size: 0.3rem;
          }
        } 
      }
  }

}
</style>